import java.util.*;
import java.lang.*;
public class ThreadDemo {
    static  int count = 0;
    static Object key = new Object();
    public static void main1(String[] args) {
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (key) {
                    while (count % 3!= 0) {
                        try {
                            key.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    key.notifyAll();
                }
            }
        },"A");

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (key) {
                    while (count % 3 != 1) {
                        try {
                            key.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.print(Thread.currentThread().getName());
                    count++;
                    key.notifyAll();
                }
            }
        },"B");

        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                synchronized (key) {
                    while(count % 3 != 2) {
                        try {
                            key.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    System.out.println(Thread.currentThread().getName());
                    count++;
                    key.notifyAll();
                }
            }
        },"C");

        t1.start();
        t2.start();
        t3.start();
    }
    static Object key1 = new Object();
    static Object key2 = new Object();
    static Object key3 = new Object();
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized(key1) {
                try {
                    key1.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println('a');
        });
        Thread t2 = new Thread(() -> {
            synchronized(key2) {
                try {
                    key2.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('b'+" ");
            synchronized (key1){
                key1.notify();
            }
        });
        Thread t3 = new Thread(() -> {
            synchronized(key3) {
                try {
                    key3.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.print('c'+" ");
            synchronized(key2) {
                key2.notify();
            }
        });
        t1.start();;
        t2.start();
        t3.start();
        Thread.sleep(100);
        synchronized(key3) {
            key3.notify();
        }
    }
}
